package problems.daily;

/**
 * 1754. 构造字典序最大的合并字符串
 * <p>https://leetcode.cn/problems/largest-merge-of-two-strings/</p>
 *
 * @author habitplus
 * @since 7:48, 2022/12/24
 */
public class DT1754 {
    public String largestMerge(String word1, String word2) {
        StringBuilder ret = new StringBuilder();
        int n1 = word1.length();
        int n2 = word2.length();
        int p1 = 0, p2 = 0;

        while (p1 < n1 || p2 < n2) {
            if (p1 == n1) {
                ret.append(word2.charAt(p2++));
            } else if (p2 == n2) {
                ret.append(word1.charAt(p1++));
            } else {
                // 后缀比较
                if (word1.substring(p1).compareTo(word2.substring(p2)) > 0) {
                    ret.append(word1.charAt(p1++));
                } else {
                    ret.append(word2.charAt(p2++));
                }
            }
        }

        return ret.toString();
    }
}
